//
//  KRSearchGetListRecipeCell.m
//  cookBook
//
//  Created by kuroken on 16/1/26.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRSearchGetListRecipeCell.h"
#import <UIImageView+WebCache.h>
#import <PureLayout.h>
#import "KRSearchGetResultList.h"
#import "KRConst.h"

// cell重用标识
static NSString * const KRSearchGetListRecipe = @"searchGetListRecipe";

@interface KRSearchGetListRecipeCell ()

/** 封面 iamgeView */
@property (nonatomic, weak) UIImageView *coverImageView;
/** 视频标记 imageView */
@property (nonatomic, weak) UIImageView *videoTagImageView;
/** 标题 label */
@property (nonatomic, weak) UILabel *titleLabelView;
/** 标签（人气、推荐） label */
@property (nonatomic, weak) UILabel *cardLabel;
/** xx收藏 xx浏览 label */
@property (nonatomic, weak) UILabel *collectionLabel;
/** 食材 label */
@property (nonatomic, weak) UILabel *stuffLabel;

@end

@implementation KRSearchGetListRecipeCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRSearchGetListRecipeCell *cell = [tableView dequeueReusableCellWithIdentifier:KRSearchGetListRecipe];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KRSearchGetListRecipe];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // <#初始化代码#>
        
        [self setupSubview];
        [self addConstraint];
    }
    return self;
}

- (void)setRecipe:(KRSearchGetResultList *)recipe
{
    _recipe = recipe;
    
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:recipe.list_cover] placeholderImage:[UIImage imageNamed:@"default_r172-118"]];
    [self.videoTagImageView setHidden:!recipe.hasVideo];
    [self.titleLabelView setText:recipe.list_title];
    [self.cardLabel setHidden:!recipe.list_card.length];
    [self.cardLabel setText:[NSString stringWithFormat:@" %@ ", recipe.list_card]];
    [self.collectionLabel setText:recipe.list_collection];
    [self.stuffLabel setText:recipe.list_stuff];
}


/**
 *  初始化子控件
 */
- (void)setupSubview
{
    /** 封面 iamgeView */
    UIImageView *coverImageView = [[UIImageView alloc] init];
    coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    coverImageView.clipsToBounds = YES;
    [self.contentView addSubview:coverImageView];
    self.coverImageView = coverImageView;

    /** 视频标记 imageView */
    UIImageView *videoTagImageView = [[UIImageView alloc] init];
    [videoTagImageView setImage:[UIImage imageNamed:@"ico_video"]];
    [videoTagImageView setSize:videoTagImageView.image.size];
    [self.coverImageView addSubview:videoTagImageView];
    self.videoTagImageView = videoTagImageView;

    /** 标题 label */
    UILabel *titleLabelView = [[UILabel alloc] init];
    [titleLabelView setFont:[UIFont systemFontOfSize:16]];
    [self.contentView addSubview:titleLabelView];
    self.titleLabelView = titleLabelView;
    
    /** 标签（人气、推荐） imageView */
    UILabel *cardLabel = [[UILabel alloc] init];
    [cardLabel setFont:[UIFont systemFontOfSize:13]];
    [cardLabel setTextColor:[UIColor whiteColor]];
    [cardLabel setBackgroundColor:[UIColor clearColor]];
    [cardLabel.layer setBackgroundColor:KRColor(111, 143, 18).CGColor];
    [cardLabel.layer setCornerRadius:2];
    [self.contentView addSubview:cardLabel];
    self.cardLabel = cardLabel;

    /** xx收藏 xx浏览 label */
    UILabel *collectionLabel = [[UILabel alloc] init];
    [collectionLabel setFont:[UIFont systemFontOfSize:13]];
    [collectionLabel setTextColor:[UIColor grayColor]];
    [self.contentView addSubview:collectionLabel];
    self.collectionLabel = collectionLabel;

    /** 食材 label */
    UILabel *stuffLabel = [[UILabel alloc] init];
    [stuffLabel setFont:[UIFont systemFontOfSize:13]];
    [stuffLabel setTextColor:[UIColor grayColor]];
    [self.contentView addSubview:stuffLabel];
    self.stuffLabel = stuffLabel;
}

/**
 *  添加约束
 */
- (void)addConstraint
{
    [self.coverImageView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [self.coverImageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:12];
    [self.coverImageView autoSetDimensionsToSize:CGSizeMake(90, 65)];
    
    [self.videoTagImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:5];
    [self.videoTagImageView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:5];
    
    [self.titleLabelView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.coverImageView];
    [self.titleLabelView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.coverImageView withOffset:10];
//    [self.titleLabelView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    
    [self.cardLabel autoAlignAxis:ALAxisHorizontal toSameAxisOfView:self.titleLabelView];
    [self.cardLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.titleLabelView withOffset:10];
    
    [self.collectionLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabelView withOffset:7];
    [self.collectionLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.titleLabelView];
    
    [self.stuffLabel autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.coverImageView];
    [self.stuffLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.titleLabelView];
    [self.stuffLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
//    [self.stuffLabel autoSetDimension:ALDimensionHeight toSize:16];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
